/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainPanel2.java
 *
 * Created on Apr 2, 2012, 2:06:35 PM
 */
package pacificlms.MainPanel;

import javax.swing.plaf.TabbedPaneUI;
import pacificlms.LoggedMember;
import java.lang.reflect.Method;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import pacificlms.DataHolder;
import pacificlms.Error.ErrorHandle;
import pacificlms.Verifiable.VerifiableObject;
import pacificlms.Verifiable.Database.Member;

/**
 *
 * @author brianhhh
 */
public class MainPanel extends javax.swing.JPanel {

    /** Creates new form MainPanel2 */
    public MainPanel() {
        initComponents();
        SingletonMainPanel = this;
    }

    public void AddCentralUIPanel(CentralUIPanel CUIP)
    {
        int index = pnlCentralUI.indexOfComponent(CUIP);
        if (index < 0)//if it does not exist
        {
            pnlCentralUI.addTab(CUIP.TabName(), CUIP);
            CUIP.setVisible(true);
            pnlCentralUI.setSelectedIndex(pnlCentralUI.indexOfComponent(CUIP));
        }
        else
        {
            //tab already exists
        }
    }
    public void AddCentralUIPanel(CentralUIPanel CUIP, CentralUIPanel Requestor, Method Call)//call this method in requestor with the verifiable object of the new cuip
    {
        AddCentralUIPanel(CUIP);
        
        boolean BadCall = false;
        if (Call == null)
        {
            BadCall = true;
        }
        else if (Call.getReturnType() != void.class)
        {
            BadCall = true;
        }
        else if (Call.getParameterTypes().length == 0 || Call.getParameterTypes()[0] != VerifiableObject.class)
        {
            BadCall = true;
        }
        
        if (BadCall)
        {
            ErrorHandle.Display("Call Method Mistach", "Error call method mistached must accept verifiable object and return nothing.\nEmail brianhhh@mss.icics.ubc.ca if you see this message.", null, true);
            return;
        }
        
        //requestor.enabled = false
        int index = pnlCentralUI.indexOfComponent(Requestor);
        pnlCentralUI.setEnabledAt(index, false);
        
        CUIP.SetUIRequestor(Requestor, Call);
        
        //add listener for close on newCUIP to run a specific method in requestor
        //to re-enable it
    }
    
    public void RemoveCentralUIPanel(CentralUIPanel CUIP)
    {
        int index = pnlCentralUI.indexOfComponent(CUIP);
        if (index >= 0)
        {
            pnlCentralUI.removeTabAt(index);
            CUIP.setVisible(false);
            CUIP.CallUIRequestor();
        }
        else
        {
            //tab does not exist
        }
    }
    
    private void ClearCentralUIPanel()
    {
        while (pnlCentralUI.getTabCount() > 0)
        {
            pnlCentralUI.removeTabAt(0);
        }
    }
    
    private JPanel ControlPanel = null;
    public void UpdateLogin()
    {
        //change controls
        Member CurrentLog = LoggedMember.getInstance();
        
        if (ControlPanel != null)
            pnlUserControlArea.remove(ControlPanel);
        
        if (CurrentLog.IsNonmember())
            ControlPanel = new ControlPanelNonMember();
        else if (CurrentLog.IsStudent())
            ControlPanel = new ControlPanelMember();
        else if (CurrentLog.IsFaculty())
            ControlPanel = new ControlPanelMember();
        else if (CurrentLog.IsLibrarian())
            ControlPanel = new ControlPanelLibrarian();
        else
            pnlUserControlArea.add(new ControlPanelNonMember());
        
        pnlUserControlArea.add(ControlPanel);
        
        //if now not loged in remove all tabs
        if (!CurrentLog.LoggedIn())
        {
            ClearCentralUIPanel();
            lblLoginInfo.setText("Welcome guest.");
        }
        else
        {
            String SignInName = "Signed in as " + CurrentLog.Name(null);
            //fixme check length
            lblLoginInfo.setText(SignInName);
        }
    }
    
    private static MainPanel SingletonMainPanel = null;
    public static MainPanel GetMainPanel()
    {
        return SingletonMainPanel;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jRadioButton1 = new javax.swing.JRadioButton();
        pnlUserControls = new javax.swing.JPanel();
        pnlUserControlArea = new javax.swing.JPanel();
        btnHelp = new javax.swing.JButton();
        pnlLogin = new javax.swing.JPanel();
        btnLoginLogOut = new javax.swing.JButton();
        lblLoginInfo = new javax.swing.JLabel();
        pnlCentralUI = new javax.swing.JTabbedPane();

        jRadioButton1.setText("jRadioButton1");

        setMaximumSize(new java.awt.Dimension(796, 596));
        setMinimumSize(new java.awt.Dimension(796, 596));
        setPreferredSize(new java.awt.Dimension(796, 596));

        pnlUserControls.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        pnlUserControlArea.setBackground(new java.awt.Color(229, 229, 229));
        pnlUserControlArea.setRequestFocusEnabled(false);
        pnlUserControlArea.setLayout(new java.awt.BorderLayout());

        btnHelp.setText("Help");
        btnHelp.setEnabled(false);
        btnHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHelpActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlUserControlsLayout = new javax.swing.GroupLayout(pnlUserControls);
        pnlUserControls.setLayout(pnlUserControlsLayout);
        pnlUserControlsLayout.setHorizontalGroup(
            pnlUserControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlUserControlsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlUserControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pnlUserControlArea, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                    .addComponent(btnHelp, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlUserControlsLayout.setVerticalGroup(
            pnlUserControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlUserControlsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlUserControlArea, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnHelp)
                .addContainerGap())
        );

        pnlLogin.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnLoginLogOut.setText("Login / Logout");
        btnLoginLogOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginLogOutActionPerformed(evt);
            }
        });

        lblLoginInfo.setText("Login info");

        javax.swing.GroupLayout pnlLoginLayout = new javax.swing.GroupLayout(pnlLogin);
        pnlLogin.setLayout(pnlLoginLayout);
        pnlLoginLayout.setHorizontalGroup(
            pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblLoginInfo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnLoginLogOut, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        pnlLoginLayout.setVerticalGroup(
            pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnLoginLogOut)
                .addComponent(lblLoginInfo))
        );

        pnlCentralUI.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
        pnlCentralUI.setMaximumSize(new java.awt.Dimension(624, 567));
        pnlCentralUI.setMinimumSize(new java.awt.Dimension(624, 567));
        pnlCentralUI.setPreferredSize(new java.awt.Dimension(624, 567));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnlUserControls, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlCentralUI, javax.swing.GroupLayout.PREFERRED_SIZE, 620, Short.MAX_VALUE)
                    .addComponent(pnlLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlUserControls, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnlLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlCentralUI, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    void DisplayHelp()
    {
        Runtime run = Runtime.getRuntime();
        try
        {
            Runtime.getRuntime().exec("www.humenet.net/cics505/Pacific%20Team/LMS/pacific-lms_Help/index.html");
        }
        catch (Exception ex)
        {
            JOptionPane.showMessageDialog(this, "Unable to connect to official Pacific LMS online help.", "Error", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    
    private void btnHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHelpActionPerformed
        DisplayHelp();
    }//GEN-LAST:event_btnHelpActionPerformed
    
    //fixme remove
    int jButton5Count = 0;
    void LoginLogout()
    {
        if (!LoggedMember.getInstance().LoggedIn())
        {
            for(int itr = 0; itr < pnlCentralUI.getTabCount(); itr++)
            {
                if (pnlCentralUI.getComponentAt(itr).getClass() == ActionLogin.class)
                {
                    pnlCentralUI.removeTabAt(itr);
                }
            }
            AddCentralUIPanel(new ActionLogin());
        }
        else
        {
            LoggedMember.Clear();
        }
    }
    
    private void btnLoginLogOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginLogOutActionPerformed
        LoginLogout();
    }//GEN-LAST:event_btnLoginLogOutActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHelp;
    private javax.swing.JButton btnLoginLogOut;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JLabel lblLoginInfo;
    private javax.swing.JTabbedPane pnlCentralUI;
    private javax.swing.JPanel pnlLogin;
    private javax.swing.JPanel pnlUserControlArea;
    private javax.swing.JPanel pnlUserControls;
    // End of variables declaration//GEN-END:variables
}
